package com.jader.adapter;

import java.util.List;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.jader.beans.Goods;
import com.jader.util.JaderApplication;
import com.zlp.jader.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {

	// 传入javabean对象
		List<Goods> list;
		private Context context;
		// 找到布局
		private LayoutInflater mLayoutInflater;
		// 缓存
		private ViewHoder holder;
		// 图片加载 需导jar包
		private ImageLoader imageLoader;

		public MyAdapter(List<Goods> list, Context context, ImageLoader imageLoader) {
			super();
			this.list = list;
			this.context = context;
			this.imageLoader = imageLoader;
			mLayoutInflater = LayoutInflater.from(this.context);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// 加载布局
			if (arg1 == null) {
				// 如果没有布局 就布局上R.layout.adapter_list
				arg1 = mLayoutInflater.inflate(R.layout.adapter_list, null);
				// 缓存
				holder = new ViewHoder();
				holder.imageView = (NetworkImageView) arg1.findViewById(R.id.image);
				holder.titleTextView = (TextView) arg1.findViewById(R.id.title);
				holder.description = (TextView) arg1.findViewById(R.id.description);
				holder.price = (TextView) arg1.findViewById(R.id.price);
				// 设置
				arg1.setTag(holder);
			}
			holder = (ViewHoder) arg1.getTag();

			// 设置控件数据来源（存储在list集合中的JavaBean对象）
			holder.imageView.setImageUrl(JaderApplication.getIP()+list.get(arg0).getPicture(), imageLoader);
			holder.titleTextView.setText(list.get(arg0).getGoodsName());
			holder.description.setText(list.get(arg0).getDesc());
			//double类型转换为string类型
			holder.price.setText(list.get(arg0).getPrice()+"");
			// 返回布局
			return arg1;
		}

		// 缓存类
		class ViewHoder {
			// 初始化控件
			NetworkImageView imageView;
			TextView titleTextView, description, price;

		}


}
